package com.shuoyue.ycgk.ui.mine.learnprogress;

import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.baseadapter.IndexFragmentStateAdapter;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.ui.common.CommonModuleContract;
import com.shuoyue.ycgk.views.TabLayoutUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 学习进度
 */
public class LearnProgressActivity extends BaseMvpActivity implements CommonModuleContract.View {

    @BindView(R.id.back)
    ImageButton back;
    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.l_right)
    FrameLayout lRight;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;

    Type type;
    CommonModuleContract.Presenter modelPresenter;
    IndexFragmentStateAdapter indexFragmentAdapter;
    List<BaseMvpFragment> fragmentMainItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_learn_progress;
    }

    @Override
    protected void initView() {
        pageTitle.setText("学习进度");
    }

    @Override
    public void initData() {
        modelPresenter = new CommonModuleContract.Presenter();
        modelPresenter.attachView(this);
        modelPresenter.getBaseModels(1);
    }

    @Override
    public void setBaseType(List<Type> baseTypes) {
        String selectModelName = SPUtils.getLastSelectMoudle(mContext);
        if (selectModelName == null) {
            type = baseTypes.get(0);
        } else {
            for (Type baseType : baseTypes) {
                if (selectModelName.equals(baseType.getName())) {
                    type = baseType;
                    break;
                }
            }
        }
        modelPresenter.getChildType(type.getId());
    }

    @Override
    public void setTabType(List<Type> mTypes) {
        fragmentMainItems = new ArrayList<>();
        for (Type topType : mTypes) {
            fragmentMainItems.add(FragmentLearnProgress.getInstance(topType));
        }
        indexFragmentAdapter = new IndexFragmentStateAdapter(getSupportFragmentManager(), fragmentMainItems);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(1);
        viewPager.setAdapter(indexFragmentAdapter);
        viewPager.setCurrentItem(0);
        TabLayoutUtil tabLayoutUtil = new TabLayoutUtil();
        tabLayoutUtil.setTabTextSizeWhenSelect(tabLayout, mTypes);
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
